/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.dao.OutBoundTaskDao;
import me.zhengjie.dao.OutboundOrder;
import me.zhengjie.dao.OutboundOrderDetailDao;
import me.zhengjie.server.OutBoundTaskService;
import me.zhengjie.server.OutboundOrderDetailService;
import me.zhengjie.server.OutboundOrderService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.Result;
import me.zhengjie.vo.OutBoundDetailVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author hws
* @date 2025-06-06
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "出库单物料信息管理")
@RequestMapping("/api/outBoundOrderDetail")
public class OutBoundOrderDetailController {

    private final OutboundOrderService outboundOrderService;

    private final OutboundOrderDetailService outboundOrderDetailService;

    private final OutBoundTaskService outBoundTaskService;

    @GetMapping
    @Log("通过出库单号获取outBoundDetail")
    @ApiOperation("通过出库单号获取outBoundDetail")
    public OutBoundDetailVo getByOutBoundOrder(@RequestParam String outBoundOrderNo){
        OutboundOrder outboundOrder = outboundOrderService.getOne(new LambdaQueryWrapper<OutboundOrder>().eq(OutboundOrder::getOutboundOrderNo, outBoundOrderNo));
        OutBoundDetailVo outBoundDetailVo = new OutBoundDetailVo();
        ArrayList<OutboundOrderDetailDao> notMateInfo = new ArrayList<>();
        ArrayList<OutboundOrderDetailDao> materialInfo = new ArrayList<>();
        outBoundDetailVo.setOutBoundOrderNo(outBoundOrderNo);
        outBoundDetailVo.setStatus(outboundOrder.getStatus());
        outBoundDetailVo.setCreateTime(outboundOrder.getCreateTime());
        outBoundDetailVo.setOutboundType(outboundOrder.getOutboundType());
        outBoundDetailVo.setOutboundStore(outboundOrder.getOutboundStore());
        outBoundDetailVo.setOutboundPolicy(outboundOrder.getOutboundPolicy());
        //任务集合
        List<OutBoundTaskDao> taskDaoList = outBoundTaskService.list(new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getProductOutBoundOrderNo, outboundOrder.getOutboundOrderNo()));
        //完成分配的任务
        List<OutBoundTaskDao> sucessTask = taskDaoList.stream()
                .filter(e -> e.getStatus() != null && e.getStatus() ==2)
                .collect(Collectors.toList());
        for (OutBoundTaskDao outBoundTaskDao : sucessTask) {
            String materialCode = outBoundTaskDao.getMaterialCode();
            OutboundOrderDetailDao outboundOrderDetailDao = outboundOrderDetailService.getOne(new LambdaQueryWrapper<OutboundOrderDetailDao>().eq(OutboundOrderDetailDao::getMaterialCode, materialCode).eq(OutboundOrderDetailDao::getIwsOutboundOrderId, outBoundOrderNo));
            materialInfo.add(outboundOrderDetailDao);
        }
        outBoundDetailVo.setMaterialInfo(materialInfo);
//        List<OutBoundTaskDao> notTask = taskDaoList.stream().filter(e -> e.getStatus() != null && e.getStatus() == 0).collect(Collectors.toList());
//        for (OutBoundTaskDao outBoundTaskDao : notTask) {
//            String materialCode = outBoundTaskDao.getMaterialCode();
//            OutboundOrderDetailDao outboundOrderDetailDao = outboundOrderDetailService.getOne(new LambdaQueryWrapper<OutboundOrderDetailDao>().eq(OutboundOrderDetailDao::getMaterialCode, materialCode).eq(OutboundOrderDetailDao::getIwsOutboundOrderId, outBoundOrderNo));
//            notMateInfo.add(outboundOrderDetailDao);
//        }
        //未分拣出库详情
        List<OutboundOrderDetailDao> list = outboundOrderDetailService.list(new LambdaQueryWrapper<OutboundOrderDetailDao>().eq(OutboundOrderDetailDao::getIwsOutboundOrderId, outBoundOrderNo));
        outBoundDetailVo.setNotMaterialInfo(list);
        // 提取 materialInfo 中的物料编号
        Set<String> materialCodes = materialInfo.stream()
                .map(OutboundOrderDetailDao::getMaterialCode)
                .collect(Collectors.toSet());

// 过滤 list 中的物料编号
        List<OutboundOrderDetailDao> notMaterialInfo = list.stream()
                .filter(e -> !materialCodes.contains(e.getMaterialCode()))
                .collect(Collectors.toList());
        outBoundDetailVo.setNotMaterialInfo(notMaterialInfo);
        return outBoundDetailVo;
    }

















}